<template>
  <div class="user-management-container">
    <div class="spaceForm">
      <el-form :inline="true" :model="queryForm">
        <el-form-item>
          <el-date-picker
            v-model="queryForm.searchDate"
            end-placeholder="结束日期"
            start-placeholder="开始日期"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="changeDate"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.keyword"
            clearable
            placeholder="请输入关键词"
            @keyup.enter.native="queryData"
            @clear="queryData"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <el-form>
        <el-button
          icon="el-icon-delete"
          type="danger"
          @click="handleDelete($event)"
        >
          批量删除
        </el-button>
      </el-form>
    </div>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="ID"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="用户名"
        prop="user_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="昵称"
        prop="nick_name"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <div v-if="row.nick_name">{{ row.nick_name }}</div>
          <div v-else>---</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="真实姓名"
        prop="true_name"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <div v-if="row.true_name">{{ row.true_name }}</div>
          <div v-else>---</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="手机号"
        prop="user_mobile"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="性别"
        prop="user_sex"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <div v-for="(item, index) in dictList" :key="index">
            <div v-if="row.user_sex == item.item_code">
              {{ item.item_name }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="邮箱"
        prop="user_email"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <div v-if="row.user_email">{{ row.user_email }}</div>
          <div v-else>---</div>
        </template>
      </el-table-column>

      <el-table-column align="center" label="账号类型" prop="is_admin">
        <template #default="{ row }">
          <div v-if="row.is_admin == 1">管理员</div>
          <div v-else>普通用户</div>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="所属部门" prop="department_id">
        <template #default="{ row }">
          <user-dep ref="userDep" :id="row.department_id"></user-dep>
        </template>
      </el-table-column> -->
      <el-table-column
        align="center"
        label="注册时间"
        prop="create_time_format"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="操作" width="85">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
import { doDelete, getList } from '@/api/userManagement'
import { getDictByCode } from '@/utils/dict'
import Edit from './components/edit'
import userDep from './components/dep'
export default {
  name: 'UserManagement',
  components: { Edit, userDep },
  data() {
    return {
      list: [],
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      selectRows: '',
      queryForm: {
        page: 1,
        perpage: 10,
        start_time: '',
        end_time: '',
      },
      dictList: [],
    }
  },
  created() {
    this.fetchData()
    this.getDict()
  },
  methods: {
    changeDate(value) {
      if (value) {
        this.queryForm.start_time = value[0]
        this.queryForm.end_time = value[1]
      } else {
        this.queryForm.start_time = null
        this.queryForm.end_time = null
      }
      this.queryData()
    },
    setSelectRows(val) {
      this.selectRows = val
    },
    handleEdit(row) {
      if (row.id) {
        this.$refs['edit'].showEdit(row)
      } else {
        this.$refs['edit'].showEdit()
      }
    },
    async getDict() {
      this.dictList = await getDictByCode('user_sex')
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { data } = await doDelete({ user_ids: row.id })
          if (data.is_success) {
            this.$baseMessage('删除成功', 'success', 'vab-hey-message-success')
            await this.fetchData()
          }
        })
      } else {
        if (this.selectRows.length > 0) {
          const ids = this.selectRows.map((item) => item.id).join()
          this.$baseConfirm('你确定要删除选中项吗', null, async () => {
            const { data } = await doDelete({ user_ids: ids })
            if (data.is_success) {
              this.$baseMessage(
                '删除成功',
                'success',
                'vab-hey-message-success'
              )
              await this.fetchData()
            }
          })
        } else {
          this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
        }
      }
    },
    handleSizeChange(val) {
      this.queryForm.perpage = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.page = val
      this.fetchData()
    },
    queryData() {
      this.queryForm.page = 1
      this.fetchData()
    },
    async fetchData() {
      this.listLoading = true
      const {
        data: { items, total },
      } = await getList(this.queryForm)
      this.list = items
      this.total = total
      this.$forceUpdate()
      this.listLoading = false
    },
  },
}
</script>
